
/**
 ******************************************************************************
 * @file    lv_100ask_demo_qrcode.c
 * @author  百问科技
 * @version V1.0
 * @date    2021-11-14
 * @brief	动态创建二维码
 ******************************************************************************
 * Change Logs:
 * Date           Author          Notes
 * 2021-10-14     zhouyuebiao     First version
 ******************************************************************************
 * @attention
 *
 * Copyright (C) 2008-2021 深圳百问网科技有限公司<https://www.100ask.net/>
 * All rights reserved
 *
 ******************************************************************************
 */
/**
 * @file lv_100ask_demo_qrcode.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "../../lv_100ask_demo.h"

#if LV_USE_100ASK_DEMO_QRCODE

#include <stdio.h>
#include <stdlib.h>
#include "lv_100ask_demo_qrcode.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/
static lv_obj_t * g_qr;

/**********************
 *  STATIC PROTOTYPES
 **********************/




/**********************
 *   STATIC FUNCTIONS
 **********************/

 static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);
    if((code == LV_EVENT_VALUE_CHANGED) || (code == LV_EVENT_CLICKED) || (code == LV_EVENT_FOCUSED)) {
        lv_keyboard_set_textarea(kb, ta);
        lv_qrcode_update(g_qr, lv_textarea_get_text(ta), strlen(lv_textarea_get_text(ta)));
        //lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);

    }

     else if(code == LV_EVENT_READY) {
        //lv_keyboard_set_textarea(kb, NULL);
        lv_keyboard_set_textarea(kb, ta);
        lv_qrcode_update(g_qr, lv_textarea_get_text(ta), strlen(lv_textarea_get_text(ta)));
        //lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

    }
}

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/*
 *  函数名：   void lv_100ask_demo_qrcode(void)
 *  输入参数： 无
 *  返回值：   无
 *  函数作用： 应用初始化入口
*/
void lv_100ask_demo_qrcode(void)
{
    lv_obj_t * qr_code_panel;
    const char * qrcode_data = "http://www.100ask.net";

    // 展示区域风格样式
    static lv_style_t main_layout_style;
    static lv_style_t qrcode_layout_style;

    lv_style_init(&main_layout_style);
    //lv_style_set_bg_opa(&main_layout_style, 0);
    lv_style_set_radius(&main_layout_style, 0);
    lv_style_set_border_opa(&main_layout_style, 0);
    lv_style_set_bg_color(&main_layout_style, lv_color_hex(0xffffff));
    lv_style_set_pad_column(&main_layout_style, 0);
    //lv_style_set_pad_row(&main_layout_style, 0);
    lv_style_set_flex_flow(&main_layout_style, LV_FLEX_FLOW_ROW_WRAP);
    lv_style_set_pad_all(&main_layout_style, 0);
    lv_style_set_layout(&main_layout_style, LV_LAYOUT_FLEX);

    lv_style_init(&qrcode_layout_style);
    lv_style_set_bg_opa(&qrcode_layout_style, 0);
    lv_style_set_radius(&qrcode_layout_style, 0);
    lv_style_set_border_opa(&qrcode_layout_style, 0);
    lv_style_set_pad_all(&qrcode_layout_style, 0);

    // 展示区域
    lv_obj_t * obj_main_layout = lv_obj_create(lv_scr_act());  /*Content can be aded here*/
    lv_obj_set_size(obj_main_layout, LV_PCT(100), LV_PCT(100));
    lv_obj_add_style(obj_main_layout, &main_layout_style, 0);

    /* Create title */
    lv_obj_t * label_title = lv_label_create(obj_main_layout);
    lv_obj_set_width(label_title, LV_PCT(100));
    lv_obj_set_style_text_font(label_title, &lv_font_montserrat_32, 0);
    lv_obj_set_style_text_align(label_title, LV_TEXT_ALIGN_CENTER, 0);
    //lv_label_set_recolor(label_title, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_text(label_title, "CREATE QRCODE");
    lv_obj_align(label_title, LV_ALIGN_CENTER, 0, 0); //lv_obj_center(label_title);

    /* Init QRcode */
    lv_color_t bg_color = lv_palette_lighten(LV_PALETTE_LIGHT_BLUE, 5);
    lv_color_t fg_color = lv_palette_darken(LV_PALETTE_BLUE, 4);

    qr_code_panel = lv_obj_create(obj_main_layout);
    lv_obj_add_style(qr_code_panel, &qrcode_layout_style, 0);
    lv_obj_set_size(qr_code_panel, LV_PCT(50), LV_PCT(40));

    g_qr = lv_qrcode_create(qr_code_panel, 200, bg_color, fg_color);
    lv_qrcode_update(g_qr, qrcode_data, strlen(qrcode_data));        /* Set data */
    /*Add a border with bg_color*/
    lv_obj_set_style_border_color(g_qr, bg_color, 0);
    lv_obj_set_style_border_width(g_qr, 5, 0);

    lv_obj_center(g_qr);


    /*Create the text area*/
    lv_obj_t * ta = lv_textarea_create(obj_main_layout);
    lv_textarea_set_password_mode(ta, false);
    lv_obj_set_size(ta, LV_PCT(50), LV_PCT(40));
    lv_textarea_set_text(ta, qrcode_data);
    //lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, NULL);

    /*Create a keyboard to use it with an of the text areas*/
    lv_obj_t *kb = lv_keyboard_create(obj_main_layout);
    lv_keyboard_set_textarea(kb, ta); /*Focus it on one of the text areas to start*/

    lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
}


#endif /* LV_USE_100ASK_DEMO_GAME_TRON*/
